﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Atom.NetFrame;

namespace Atom.Logic
{
    /// <summary>
    /// 广播
    /// </summary>
    public class AbsMulitHandle : AbsOnceHandler
    {
        public List<UserToken> Tokens = new List<UserToken>();

        public bool Enter(UserToken ut)
        {
            if (Tokens.Contains(ut)) return false;
            Tokens.Add(ut);
            return true;
        }
        public bool IsEntered(UserToken ut)
        {
            return Tokens.Contains(ut);
        }
        public bool Level(UserToken ut)
        {
            if (!Tokens.Contains(ut)) return false;
            Tokens.Remove(ut);
            return true;
        }

        public void Broadcast(int command, byte[] message, UserToken exTokn = null)
        {
            Broadcast(GetArea(), command, message, exTokn);
        }
        public void Broadcast(int area, int command, byte[] message, UserToken exTokn = null)
        {
            Broadcast(Gettype(), GetArea(), command, message, exTokn);
        }

        public void Broadcast(byte type, int area, int command, byte[] message, UserToken exTokn = null)
        {
            SocketModel sm = new SocketModel(type, area, command, message);
            foreach (var item in Tokens)
            {
                if (item != exTokn)
                {
                    item.SendMsg(sm.Encode());
                }
            }
        }


    }
}
